const express = require(`express`)
const app = express()
const port = 1000

// 设置 CORS 头，允许跨域请求
app.use((req, res, next) => {
  res.header(`Access-Control-Allow-Origin`, `*`)
  res.header(`Access-Control-Allow-Headers`, `Origin, X-Requested-With, Content-Type, Accept`)
  next()
})

// 添加 express.json() 中间件以解析 JSON 请求
app.use(express.json())

// 定义一个登录接口
// 定义一个登录接口
app.post(`/api/login`, (req, res) => {
  const { username, password } = req.body

  if (username === `admin` && password === `123456`) {
    // 管理员信息
    const userInfo = {
      username: `admin`,
      nickname: `管理员`,
      token: `123456`,
      avatar: `https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png`, // 用户头像的 URL
      routes: [`Orders`, `Categories`, `Products`],
    }

    res.json({ code: 0, message: `登录成功`, data: userInfo })
  } else if (username === `user` && password === `123456`) {
    // 普通用户信息
    const userInfo = {
      username: `用户`,
      nickname: `用户`,
      token: `123456`,
      avatar: `https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png`, // 用户头像的 URL
      routes: [`Orders`, `Categories`, `Products`, `Information`],
    }
    res.json({ code: 0, message: `登录成功`, data: userInfo })
  } else {
    res.json({ code: 1, message: `用户名或密码错误` })
  }
})

// 启动服务器
app.listen(port, () => {
  console.log(`Mock server is running on http://localhost:${port}`)
})
